package com.javandad.app.client.project;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Label;
import com.javandad.app.shared.entity.project.Project;
import com.javandad.app.shared.entity.project.ProjectAssoc;

public class HomeTab extends AbsolutePanel {

	/**
	 * @param owner
	 * @param username
	 * @param projectName
	 * @param projectService
	 * @wbp.parser.constructor
	 */
	public HomeTab(final ProjectServiceAsync projectService,
			final String projectName, final String username, boolean owner) {
		this.projectService = projectService;
		this.projectName = projectName;
		this.username = username;
		this.owner = owner;

		this.setSize("600px", "600px");

		this.lblName = new Label("Unknown");
		add(lblName, 110, 33);

		this.lblType = new Label("Unknown");
		add(lblType, 110, 69);

		this.lblDescription = new Label("Unknown");
		add(lblDescription, 110, 106);
		lblDescription.setSize("438px", "233px");

		chckbxIWouldLike = new CheckBox("I would like to follow this project");
		chckbxIWouldLike
				.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
					public void onValueChange(ValueChangeEvent<Boolean> event) {
						projectService.followProject(projectName, username,
								event.getValue(), new AsyncCallback<Boolean>() {

									@Override
									public void onFailure(Throwable caught) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onSuccess(Boolean result) {
										// TODO Auto-generated method stub

									}
								});
					}
				});
		add(chckbxIWouldLike, 29, 356);

		Label lblName_1 = new Label("Name");
		add(lblName_1, 29, 33);

		Label lblType = new Label("Type");
		add(lblType, 29, 69);

		Label lblDescription_1 = new Label("Description");
		add(lblDescription_1, 29, 106);
		this.setVisible(true);
	}

	@Override
	public void setVisible(boolean visible) {
		super.setVisible(visible);
		if (visible) {
			projectService.getProject(projectName,
					new AsyncCallback<Project>() {

						@Override
						public void onSuccess(Project result) {
							if (result != null) {
								lblName.setText(result.getName());
								lblDescription.setText(result.getDescription());
								switch (result.getProjectType()) {
								case Project.TYPE_LIBRARY:
									lblType.setText("Library");
									break;
								case Project.TYPE_APPLICATION:
									lblType.setText("Application");
									break;
								}

							}

						}

						@Override
						public void onFailure(Throwable caught) {
						}
					});

			projectService
					.getThisAssociationAmongAllIncludingUnconfirmedInvitations(
							projectName, username, 0,
							new AsyncCallback<ProjectAssoc>() {

								@Override
								public void onFailure(Throwable caught) {
								}

								@Override
								public void onSuccess(ProjectAssoc result) {
									if (result != null) {
										if ((result.getLevel() | ProjectAssoc.FOLLOWING_PROJECT) == result
												.getLevel()) {
											chckbxIWouldLike.setValue(true);
										}

									}

								}
							});
		}


	}

	
	
	
	private ProjectServiceAsync projectService;
	private String projectName;
	private String username;
	@SuppressWarnings("unused")
	private boolean owner;


	private Label lblName;
	private Label lblDescription;
	private Label lblType;
	private CheckBox chckbxIWouldLike;
}
